package com.link.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.Map;

/**
 * @ClassName SpringContextHolder
 * @Description TODO
 * @Author linK
 * @Date 2023/12/6 11:28
 * @Version 1.0
 **/
@Component
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
        return SpringContextHolder.applicationContext.getBeansWithAnnotation(annotationType);
    }

    public static boolean containsBean(String name) {
        return SpringContextHolder.applicationContext.containsBean(name);
    }

    public static Object getBean(String s) throws BeansException {
        return SpringContextHolder.applicationContext.getBean(s);
    }


    public static <T> T getBean(String s, Class<T> aClass) throws BeansException {
        return SpringContextHolder.applicationContext.getBean(s, aClass);
    }

    public static Object getBean(String s, Object... objects) throws BeansException {
        return SpringContextHolder.applicationContext.getBean(s, objects);
    }


    public static <T> T getBean(Class<T> aClass) throws BeansException {
        return SpringContextHolder.applicationContext.getBean(aClass);
    }


    public static <T> T getBean(Class<T> aClass, Object... objects) throws BeansException {
        return SpringContextHolder.applicationContext.getBean(aClass, objects);
    }


}